<?php
if(!interface_exists('XRequest', false)){
	require_once(XFRAMEWORK_DIR.'/core/mvc/http/XRequest.php');
}
class XServletRequest implements XRequest{
	private static $instance = null;
	
	private function __construct(){
		
	}
	
	/**
	 * @return XServletRequest
	 */
	static function singleton() {
		if (!self::$instance) {
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	/**
	 * @return mixed
	 */		
	public function get($key){
		return x_array_get($_GET, $key);
	}
	
	/**
	 * @return array
	 */		
	public function gets(){
		return $_GET;
	}
	
	/**
	 * @return mixed
	 */	
	public function post($key){
		return x_array_get($_POST, $key);
	}

	/**
	 * @return array
	 */	
	public function posts(){
		return $_POST;
	}
	
	/**
	 * @return mixed
	 */		
	public function header($key){
		$headers = $this->headers();
		return x_array_get($headers, $key);
	}	
	
	/**
	 * @return array
	 */		
	public function headers(){
	    if(function_exists("apache_request_headers")){
    		return apache_request_headers();
   		}
    	$headers = array();
    	foreach($_SERVER as $key => $value){
    		if(preg_match("/^HTTP_/i", $key)){
				$newKey = preg_replace("/^http-/i", "", preg_replace("/_([a-z])/e", "'-'.strtoupper('\\1')", strtolower($key)));
				$headers[$newKey] = $value;
    		}
    	}
    	return $headers;
	}
	
	/**
	 * @return mixed
	 */	
	public function method(){
		return x_array_get($_SERVER, 'REQUEST_METHOD');
	}
	
	/**
	 * @return bool
	 */	
	public function isAjax(){
		return strtoupper(x_array_get($_SERVER, 'HTTP_X_REQUESTED_WITH')) == 'XMLHTTPREQUEST';
	}
	
	/**
	 * @return bool
	 */		
	public function isGet(){
		return strtoupper($this->method()) == 'GET';
	}
	
	/**
	 * @return bool
	 */		
	public function isPost(){
		return strtoupper($this->method()) == 'POST';
	}	
	
	/**
	 * @return bool
	 */		
	public function isPut(){
		return strtoupper($this->method()) == 'PUT';
	}
	
	/**
	 * @return string
	 */		
	public function uri(){
		return x_array_get($_SERVER, 'REQUEST_URI');
	}
	
}
?>